package cn.edu.tsinghua.smxq.mobilecity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueryPriceServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312");
		output = response.getWriter();
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");

		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			result = sql.executeQuery("SELECT * FROM foodprice WHERE foodId='"
					+ foodId + "' AND cityId='" + cityId + "'");

			String priceValue = "";
			while (result.next()) { // process results one row at a time
				// userIdValue1,cityIdValue1,foodIdValue1,priceValue1,priceDateValue1

				priceValue = result.getString(4);

			}
			output.write(priceValue);

			sql.close();
			con.close();

		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());

			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312");
		output = response.getWriter();
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");

		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			result = sql.executeQuery("SELECT * FROM foodprice WHERE foodId='"
					+ foodId + "' AND cityId='" + cityId + "'");

			String priceValue = "";
			while (result.next()) { // process results one row at a time
				// userIdValue1,cityIdValue1,foodIdValue1,priceValue1,priceDateValue1

				priceValue = result.getString(4);

			}
			output.write(priceValue);

			sql.close();
			con.close();

		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());

			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}

	}
}
